package com.diy.sigmund.diycommon.designpatterns.observer;

import java.util.Observable;
import java.util.Observer;
import java.util.StringJoiner;

/**
 * @author ylm-sigmund
 * @since 2022/1/14 15:17
 */
public class CurrentConditionsDisplayOne implements Observer, DisplayElement {

    private float temperature;
    private float humidity;
    private Observable observable;

    public CurrentConditionsDisplayOne(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void display() {
        String s = new StringJoiner(", ", CurrentConditionsDisplayOne.class.getSimpleName() + "[", "]")
                .add("temperature=" + temperature)
                .add("humidity=" + humidity)
                .toString();
        System.out.println(s);
    }

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherDataOne) {
            WeatherDataOne weatherDataOne = (WeatherDataOne) o;
            this.temperature = weatherDataOne.getTemperature();
            this.humidity = weatherDataOne.getHumidity();
            display();
        }
    }
}
